package com.br.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.br.modelo.dao.UserDao;
import com.br.modelo.dao.UserLogadoDao;

public class UserControl {
	UserDao userDao = new UserDao();
	UserLogadoDao userLogado = new UserLogadoDao();

	public void createUser(String nmUser, String use, String password, String admin, int cdSetor) {
		try{
			userDao.saveUser(nmUser, use, password, admin, cdSetor);
		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}
	}

	public void inativaUser(String use) {
		try{
			userDao.inativaUser(use);
		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}		
	}

	public String logar(String user, String password) {
		String log = "";

		try{
			log = userLogado.logaUser(user, password);

		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}
		return log;
	}

	public String verificaAdmin(String user) {
		String admin = null;
		try{

			admin = userLogado.verificaAdmin(user);
		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}
		return admin;
	}

	public void logOff(String user, String data) {
		try{			       
			Date date = null;  
			try {  
				DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy - hh:mm:ss");  
				date = (java.util.Date)formatter.parse(data);  
			} catch (ParseException e) {              
				throw e;  
			}
			userLogado.logoffUser(user, date);
		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}
	}

	public void mudaSenha(String use, String password) {
		try{
			userDao.mudaSenha(use, password);
		}catch (Exception e) {
			System.out.println("Erro!"+e);
		}
	}

}
